package com.lis.springboot_lis.controller;

import com.lis.springboot_lis.pojo.ResponseMessage;
import com.lis.springboot_lis.pojo.User;
import com.lis.springboot_lis.pojo.dto.UserDto;
import com.lis.springboot_lis.service.UserService;
import jakarta.persistence.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController //接口方法返回对象，转换成json文本
@RequestMapping("/user") //localhost:8088/user/**
public class UserController {

    @Autowired
    UserService userService;
    //Rest风格接口
    //增加  //URL: localhost:8088/user/
    @PostMapping
    public ResponseMessage<User> add(@Validated @RequestBody UserDto user){
        User userNew = userService.add(user);
        return ResponseMessage.success(userNew);
    }



    //查询
    // URL: localhost:8088/user/1
    @GetMapping("/{userId}")
    public ResponseMessage<User> get(@PathVariable("userId") Integer userId){
        User userNew = userService.getUser(userId);
        return ResponseMessage.success(userNew);
    }


    //修改
    @PutMapping
    public ResponseMessage<User> edit(@Validated @RequestBody UserDto user){
            User userNew = userService.edit(user);
            return ResponseMessage.success(userNew);    }


    //删除用户
    @DeleteMapping("/{userId}")
    public ResponseMessage<User> delete(@PathVariable("userId") Integer userId){
        userService.delete(userId);
        return ResponseMessage.success(null);
    }


}